package com.doupi.partner.manager;

import com.doupi.partner.dal.model.Administrator;
import com.doupi.partner.model.BusinessRelation;
import com.doupi.partner.service.AdministratorService;
import com.doupi.partner.service.BusinessRelationService;
import com.doupi.partner.service.SystemConfigService;
import com.doupi.partner.service.dto.BusinessRelationDto;
import com.doupi.partner.util.ObjectUtils;
import com.doupi.partner.util.RuntimeBusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 合伙人关系 manager
 *
 * @author Alvin Wang
 * @version 1.0.0 @ 2017-04-16
 */
@Component
public class BusinessRelationManager {
	private final AdministratorService administratorService;

	private final SystemConfigService systemConfigService;

	private final AdministratorManager administratorManager;

	private final BusinessRelationService businessRelationService;

	@Autowired
	public BusinessRelationManager(AdministratorService administratorService, SystemConfigService systemConfigService, AdministratorManager administratorManager,
			BusinessRelationService businessRelationService) {
		this.administratorService = administratorService;
		this.systemConfigService = systemConfigService;
		this.administratorManager = administratorManager;
		this.businessRelationService = businessRelationService;
	}

	/**
	 * 将List<BusinessRelation>转为 List<BusinessRelationDto> lists
	 */
	public List<BusinessRelation> convertBusinessRelationListToDtoList(List<BusinessRelation> records) {
		List<BusinessRelation> lists = new ArrayList<>();
		for (BusinessRelation business : records) {
			lists.add(convertBusinessRelationToDto(business));
		}
		return lists;
	}

	/**
	 * 将BusinessRelation转为BusinessRelationDto
	 */
	public BusinessRelationDto convertBusinessRelationToDto(BusinessRelation business) {
		BusinessRelationDto businessRelationDto = new BusinessRelationDto();
		ObjectUtils.copyProperties(businessRelationDto, business);
		businessRelationDto.setAdministrator(administratorService.selectByPrimaryKey(business.getAdministratorId()));
		businessRelationDto.setPartnerAdministrator(administratorService.selectByPrimaryKey(business.getPartnerAdministratorId()));
		businessRelationDto.setSuperiorPartnerAdministrator(administratorService.selectByPrimaryKey(business.getSubstationAdministratorId()));
		businessRelationDto.setCityAdministrator(administratorService.selectByPrimaryKey(business.getCityAdministratorId()));
		businessRelationDto.setSubstationAdministrator(administratorService.selectByPrimaryKey(business.getSubstationAdministratorId()));
		businessRelationDto.setCityConfig(systemConfigService.selectByPrimaryKey(business.getCity()));
		return businessRelationDto;
	}

	/**
	 * 保存商家关系，同时关联管理员账号
	 *
	 * @param mobile
	 *            手机号
	 * @param name
	 *            姓名
	 * @param businessRelation
	 *            关系
	 * @return 影响记录数
	 */
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public int saveBusinessRelation(String mobile, String name, BusinessRelation businessRelation, String password) {
		Administrator administrator = administratorService.selectByMobile(mobile);
		if (null != administrator && !administrator.getId().equals(businessRelation.getAdministratorId())) {
			throw RuntimeBusinessException.createException("手机号已经存在");
		}
		if (null == businessRelation.getAdministratorId()) { // 没有管理员账号，初始化一个
			businessRelation.setName(name);
			businessRelation.setMobile(mobile);
			administrator = administratorManager.initAdministrator(mobile, name, "business", password);
			businessRelation.setAdministratorId(administrator.getId());
		} else { // 修改管理员账号信息
			administratorService.updateAdministratorNameAndMobile(businessRelation.getAdministratorId(), name, mobile);
		}
		return businessRelationService.saveBusinessRelation(businessRelation);
	}
}
